﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Logic; 

namespace YouHP.Pages.TreatmentHistory
{
    public class ChangeDosage : PatientBasePage
    {

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected TreatmentInfo TrmData;
        protected TreatmentHistoryInfo TrmHistoryData;
        protected double OldDataValue;
        protected int bCurrentInfo = 0;

        protected new void HandleCommon()
        {
            this.AddLocation("治疗记录", "/TreatmentHistory/Index.aspx");
            this.AddLocation("修改剂量", "/TreatmentHistory/ChangeDosage.aspx");
            this.SetTitle("修改剂量");

            bCurrentInfo = RequestTools.GetInt("bCur", 0);
            int trmid = RequestTools.GetInt("trmid", 0);
            if (trmid <= 0)
            {
                this.AddErrLine("参数错误！");
                return;
            }

            TreatmentHistoryLogic trmHisLogic = new TreatmentHistoryLogic();
            TrmHistoryData = trmHisLogic.GetCurrentTreatment(PrData.Id, trmid);

            if (TrmHistoryData.Id <= 0)
            {
                this.AddErrLine("无效的疾病历史信息");
                return;
            }
            if (TrmHistoryData.PRId != PrData.Id)   // 攻击
            {
                this.AddErrLine("无效的疾病历史信息");
                return;
            }

            OldDataValue = TrmHistoryData.DataValue;

            TrmData = TreatmentBLL.Instance.GetTreatmentInfo(TrmHistoryData.TreatmentId);

            if (TrmData.Id <= 0)
            {
                this.AddErrLine("错误的参数");
                return;
            }

        }

        protected new void HandleGet()
        {
        }

        protected new void HandlePost()
        {
            TreatmentHistoryInfo info = this.ValidateForm();
            if (info == null) return;
            bool succ = true;
#if GENDATA
            
#else
            PatientTreatmentBLL.Instance.ChangeTreatmentDosage(info);
#endif
            if (succ)
            {
                //添加动态
                UserActionInfo _UserAction = new UserActionInfo();
                _UserAction.UserId = UserData.Id;
                _UserAction.UserName = UserData.UserName;
                _UserAction.ActionTime = DateTime.Now;
                _UserAction.ActionType = Constants.ACTION_TREATMENT_CHANGE;
                _UserAction.ActionContent = "&nbsp;<a href=\"/Pages/Alpha/Treatment.aspx?id=" + info.TreatmentId + "\"target=\"_blank\">" + TrmData.Name + "</a><font color=red>(从" + OldDataValue + TrmData.MeasurementUnits + "/" + TrmData.Frequency + "到" + info.DataValue + TrmData.MeasurementUnits + "/" + TrmData.Frequency + ")</font>&nbsp;";
                UserActionBLL.Instance.AddUserAction(_UserAction);

                this.AddMsgLine("修改成功");
            }
            else
            {
                this.AddErrLine("修改失败");
            }

            this.SetUrl("/TreatmentHistory/List.aspx?trmid=" +info.TreatmentId);
            this.SetMetaRefresh();
        }

        protected TreatmentHistoryInfo ValidateForm()
        {
            TreatmentHistoryInfo info = TrmHistoryData;

            string value = RequestTools.GetFormString("startdate");
            if (value == "")
            {
                this.AddErrLine("请填写改变剂量时间");
                return null;
            }

            DateTime date;
            bool succ = DateTime.TryParse(value, out date);
            if (succ == false)
            {
                this.AddErrLine("错误的时间 " + value);
                return null;
            }
            info.StartDate = date;

            //对时间进行检查


            info.DataValue = RequestTools.GetFormDouble("dosage", -1.0);
            if (info.DataValue < 0)
            {
                this.AddErrLine("请输入新的剂量！" + value);
                return null;
            }

            info.UpdateDate = DateTime.Now;
            info.State = Constants.CHANGE_DOSAGE;
            info.PRId = PrData.Id;
            info.IsUsing = 1;

            return info;

        }
    }
}